// This sample lists the viewer's albums.
//
// In order for this sample to work, the application must have permission 
// to access the user's photos!
function init() {
   var req = opensocial.newDataRequest();
   var idspec = opensocial.newIdSpec({'userId':'VIEWER'});
   req.add(req.newFetchAlbumsRequest(idspec),'albums');
   req.send(callback);
}

function callback(resp) {
   if (resp.hadError()) {
      debug.say("*** error: " + resp.getErrorMessage());
      // ...handle error here...
      return;
   }

   debug.say("Response received successfully. Listing albums.");
   var albums = resp.get('albums').getData().asArray();
   debug.say(albums.length + " albums returned.");
   for (var i in albums) {
      debug.say("Album ID " + albums[i].getId());

      var title = albums[i].getField(opensocial.Album.Field.TITLE);
      var desc = albums[i].getField(opensocial.Album.Field.DESCRIPTION);
      var itemcount = albums[i].getField(opensocial.Album.Field.MEDIA_ITEM_COUNT);
      var thumb = albums[i].getField(opensocial.Album.Field.THUMBNAIL_URL);

      debug.say("  title: " + title);
      debug.say("  description: " + desc);
      debug.say("  media items: " + itemcount);
      debug.say("  thumb: " + thumb);

   }
}

gadgets.util.registerOnLoadHandler(init);
